/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "key_task.h"
#include "debug_task.h"
#include "iap_task.h"
#include "adc.h"
#include "tim.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
typedef StaticTask_t osStaticThreadDef_t;
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

/* USER CODE END Variables */
/* Definitions for keyTask */
osThreadId_t keyTaskHandle;
uint32_t keyTaskBuffer[ 128 ];
osStaticThreadDef_t keyTaskControlBlock;
const osThreadAttr_t keyTask_attributes = {
  .name = "keyTask",
  .cb_mem = &keyTaskControlBlock,
  .cb_size = sizeof(keyTaskControlBlock),
  .stack_mem = &keyTaskBuffer[0],
  .stack_size = sizeof(keyTaskBuffer),
  .priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for usbTask */
osThreadId_t usbTaskHandle;
uint32_t usbTaskBuffer[ 512 ];
osStaticThreadDef_t usbTaskControlBlock;
const osThreadAttr_t usbTask_attributes = {
  .name = "usbTask",
  .cb_mem = &usbTaskControlBlock,
  .cb_size = sizeof(usbTaskControlBlock),
  .stack_mem = &usbTaskBuffer[0],
  .stack_size = sizeof(usbTaskBuffer),
  .priority = (osPriority_t) osPriorityHigh,
};
/* Definitions for iapTask */
osThreadId_t iapTaskHandle;
uint32_t iapTaskBuffer[ 512 ];
osStaticThreadDef_t iapTaskControlBlock;
const osThreadAttr_t iapTask_attributes = {
  .name = "iapTask",
  .cb_mem = &iapTaskControlBlock,
  .cb_size = sizeof(iapTaskControlBlock),
  .stack_mem = &iapTaskBuffer[0],
  .stack_size = sizeof(iapTaskBuffer),
  .priority = (osPriority_t) osPriorityHigh,
};
/* Definitions for debugTask */
osThreadId_t debugTaskHandle;
uint32_t debugTaskBuffer[ 512 ];
osStaticThreadDef_t debugTaskControlBlock;
const osThreadAttr_t debugTask_attributes = {
  .name = "debugTask",
  .cb_mem = &debugTaskControlBlock,
  .cb_size = sizeof(debugTaskControlBlock),
  .stack_mem = &debugTaskBuffer[0],
  .stack_size = sizeof(debugTaskBuffer),
  .priority = (osPriority_t) osPriorityLow,
};
/* Definitions for usbQueue */
osMessageQueueId_t usbQueueHandle;
const osMessageQueueAttr_t usbQueue_attributes = {
  .name = "usbQueue"
};
/* Definitions for iap_BinarySem */
osSemaphoreId_t iap_BinarySemHandle;
const osSemaphoreAttr_t iap_BinarySem_attributes = {
  .name = "iap_BinarySem"
};

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

void key_Task(void *argument);
void usb_Task(void *argument);
void iap_Task(void *argument);
void debug_Task(void *argument);

extern void MX_LWIP_Init(void);
void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/* Hook prototypes */
void configureTimerForRunTimeStats(void);
unsigned long getRunTimeCounterValue(void);

/* USER CODE BEGIN 1 */
/* Functions needed when configGENERATE_RUN_TIME_STATS is on */
uint32_t RUN_Time=0;
void configureTimerForRunTimeStats(void)
{
    RUN_Time=0;
    MX_TIM14_Init();
    HAL_TIM_Base_Start_IT(&htim14);
}

unsigned long getRunTimeCounterValue(void)
{
    return RUN_Time;
}
/* USER CODE END 1 */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* Create the semaphores(s) */
  /* creation of iap_BinarySem */
  iap_BinarySemHandle = osSemaphoreNew(1, 0, &iap_BinarySem_attributes);

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
//    osSemaphoreAcquire(iap_BinarySemHandle,20);
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* Create the queue(s) */
  /* creation of usbQueue */
  usbQueueHandle = osMessageQueueNew (10, sizeof(uint8_t*), &usbQueue_attributes);

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* creation of keyTask */
  keyTaskHandle = osThreadNew(key_Task, NULL, &keyTask_attributes);

  /* creation of usbTask */
  usbTaskHandle = osThreadNew(usb_Task, NULL, &usbTask_attributes);

  /* creation of iapTask */
  iapTaskHandle = osThreadNew(iap_Task, NULL, &iapTask_attributes);

  /* creation of debugTask */
  debugTaskHandle = osThreadNew(debug_Task, NULL, &debugTask_attributes);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

  /* USER CODE BEGIN RTOS_EVENTS */
  /* add events, ... */
  /* USER CODE END RTOS_EVENTS */

}

/* USER CODE BEGIN Header_key_Task */
/**
  * @brief  Function implementing the keyTask thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_key_Task */
__weak void key_Task(void *argument)
{
  /* USER CODE BEGIN key_Task */
  /* Infinite loop */
  for(;;)
  {
    osDelay(1000);
  }
  /* USER CODE END key_Task */
}

/* USER CODE BEGIN Header_usb_Task */
/**
* @brief Function implementing the usbTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_usb_Task */
__weak void usb_Task(void *argument)
{
  /* USER CODE BEGIN usb_Task */
  /* Infinite loop */
  for(;;)
  {
    osDelay(1000);
  }
  /* USER CODE END usb_Task */
}

/* USER CODE BEGIN Header_iap_Task */
/**
* @brief Function implementing the iapTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_iap_Task */
__weak void iap_Task(void *argument)
{
  /* USER CODE BEGIN iap_Task */
  /* Infinite loop */
  for(;;)
  {
    osDelay(1000);
  }
  /* USER CODE END iap_Task */
}

/* USER CODE BEGIN Header_debug_Task */
/**
* @brief Function implementing the debugTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_debug_Task */
__weak void debug_Task(void *argument)
{
  /* USER CODE BEGIN debug_Task */
  /* Infinite loop */
  for(;;)
  {
    osDelay(1000);
  }
  /* USER CODE END debug_Task */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */

/* USER CODE END Application */

